﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;


class SplitInTwoArrays
{

    static void Split()
    {
        byte[] allBytes = File.ReadAllBytes("../../ILINA.jpg");
        int bytesLength = allBytes.Length;

        using (var streamOne = File.OpenWrite("../../ilina1.bin"))
        {
            using (var streamTwo = File.OpenWrite("../../ilina2.bin"))
            {
                int i =0,k =5;
                while(i < bytesLength)
                {
                    k = 5;
                    while (k-- > 0 && i < bytesLength)
                    {
                        streamOne.WriteByte(allBytes[i++]);
                    }

                    k = 5;
                    while (k-- > 0 && i < bytesLength)
                    {
                        streamTwo.WriteByte(allBytes[i++]);
                    }
                }
            }
        }
    }

    static void Concatenate()
    {
        byte[] firstPortion = File.ReadAllBytes("../../ilina1.bin");
        byte[] secondPortion = File.ReadAllBytes("../../ilina2.bin");

        int fullLenght = firstPortion.Length + secondPortion.Length;
        byte[] recoveredBytes = new byte[fullLenght];

        int firstIndex=0, secondIndex = 0;
        int k=5, i = 0;
        while(i < fullLenght)
        {
            k=5;
            while (k-- > 0 && i < fullLenght)
            {
                recoveredBytes[i++] = firstPortion[firstIndex++];
            }

            k = 5;
            while (k-- > 0 && i < fullLenght)
            {
                recoveredBytes[i++] = secondPortion[secondIndex++];
            }
        }

        using (var stream = File.Create("../../ilinarecovery.jpg"))
        {
            stream.Write(recoveredBytes, 0, fullLenght);
        }
    }

    static void Main(string[] args)
    {
        //Split();
        Concatenate();
    }
}

